package com.ybc.chinese.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ybc.chinese.R;
import com.ybc.chinese.widget.MyWebView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <p><p/>
 * Created by Yubch on 17/2/13.
 */
public class WebActivity extends BaseActivity {

    public static final String URL = "url";
    public static final String TITLE = "title";
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.wv)
    MyWebView wv;
    @Bind(R.id.iv_left)
    ImageView ivLeft;

    private String url;
    private String mTitle;


    public static void newIntent(Context context, String url) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(URL, url);
        context.startActivity(intent);
    }

    public static void newIntent(Context context, String url, String title) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra(TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        url = getIntent().getStringExtra(URL);
        mTitle = getIntent().getStringExtra(TITLE);
        ivLeft.setImageResource(R.mipmap.ic_back);
        ivLeft.setVisibility(View.VISIBLE);
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if(TextUtils.isEmpty(mTitle)) {
                    tvTitle.setText(title);
                } else {
                    tvTitle.setText(mTitle);
                }
            }
        });
        wv.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.iv_left)
    public void back() {
        finish();
    }
}
